package com.alander.nexusgo.itinerary.controller;

import com.alander.nexusgo.common.model.dto.ItineraryDetailListDTO;
import com.alander.nexusgo.common.model.vo.ItineraryDetailVO;
import com.alander.nexusgo.common.response.Result;
import com.alander.nexusgo.framework.web.starter.controller.BaseController;
import com.alander.nexusgo.itinerary.model.entity.ItineraryDetail;
import com.alander.nexusgo.itinerary.service.ItineraryDetailService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.getPageInfo;
import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.startPage;

/**
* 行程详细安排管理
*/
@RestController
@RequestMapping("/api/itinerary-details")
public class ItineraryDetailController extends BaseController {
    
    @Autowired
    private ItineraryDetailService itineraryDetailService;
    
    /**
     * 创建行程详情
     */
    @PostMapping
    public Result<ItineraryDetailVO> create(@RequestBody ItineraryDetail itineraryDetail) {
        try {
            ItineraryDetailVO result = itineraryDetailService.createItineraryDetail(itineraryDetail, getUserId());
            return success(result);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }
    
    /**
     * 获取行程详情
     */
    @GetMapping("/{id}")
    public Result<ItineraryDetailVO> get(@PathVariable(value = "id") Long id) {
        ItineraryDetailVO itineraryDetail = itineraryDetailService.getItineraryDetailById(id);
        if (itineraryDetail == null) {
            return fail("行程详情不存在");
        }
        return success(itineraryDetail);
    }
    
    /**
     * 更新行程详情
     */
    @PutMapping("/{id}")
    public Result<ItineraryDetailVO> update(@PathVariable(value = "id") Long id, @RequestBody ItineraryDetail itineraryDetail) {
        try {
            ItineraryDetailVO result = itineraryDetailService.updateItineraryDetail(id, itineraryDetail, getUserId());
            return success(result);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }
    
    /**
     * 删除行程详情
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable(value = "id") Long id) {
        try {
            boolean result = itineraryDetailService.deleteItineraryDetail(id, getUserId());
            if (result) {
                return success();
            } else {
                return fail("删除失败");
            }
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }
    
    /**
     * 分页查询行程详情列表
     */
    @GetMapping
    public PageInfo<ItineraryDetailVO> list(@RequestBody ItineraryDetailListDTO itineraryDetailListDTO) {
        startPage(itineraryDetailListDTO);
        List<ItineraryDetailVO> itineraryDetailVOList = itineraryDetailService.listItineraryDetails(itineraryDetailListDTO);
        return getPageInfo(itineraryDetailVOList);
    }
}